import util
import psycopg2

conn = psycopg2.connect(
    host="127.0.0.1",
    database="beijing",
    user="postgres",
    password="egova"
)
query_sql = """
        SELECT 
            f_table_name as table_name,
            type as geometry_type,
            srid as srid
        FROM geometry_columns
        WHERE f_table_schema NOT IN ('information_schema', 'pg_catalog')
    """
names = []
try:
    # 使用可滚动游标
    with conn.cursor() as cursor:
        cursor.execute(query_sql)
        rows = cursor.fetchall()
        for row in rows:
            table_name, type, srid = row
            if "_" in table_name:
                print("alter table %s rename to %s;" % (table_name, table_name.split("_")[-1]))
                cursor.execute("alter table %s rename to %s" % (table_name, table_name.split("_")[-1]))
    conn.commit()
    conn.close()
except Exception as e:
    conn.rollback()
    print(f"重命名表失败: {e}")
finally:
    conn.close()
